package com.taotao.manage.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;

/**
 * 内容分类管理
 * 
 * @author joedy
 *
 */
@Controller
@RequestMapping("content/category")
public class ContentCategoryController {

	@Autowired
	private ContentCategoryService contentCategoryService;

	/**
	 * 初始化页面的分类结构
	 * 
	 * @param parentId
	 * @return
	 */
	@GetMapping
	public ResponseEntity<List<ContentCategory>> queryItemCatList(
			@RequestParam(value = "id", defaultValue = "0") Long parentId) {

		try {
			// 根据parentId查询内容分类
			ContentCategory record = new ContentCategory();
			record.setParentId(parentId);
			record.setStatus(1);
			List<ContentCategory> contentCategoryList = this.contentCategoryService.queryListByWhere(record);
			if (CollectionUtils.isEmpty(contentCategoryList)) {
				return ResponseEntity.notFound().build();
			}
			return ResponseEntity.ok(contentCategoryList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 添加节点
	 * @param parentId
	 * @param name
	 * @return
	 */
	@PostMapping
	public ResponseEntity<ContentCategory> saveContentCategory(@RequestParam("parentId") Long parentId,
			@RequestParam("name") String name) {
		try {
			// 新增当前节点
			ContentCategory record = new ContentCategory();
			record.setIsParent(false);
			record.setName(name);
			record.setParentId(parentId);
			record.setSortOrder(1);
			record.setStatus(1);
			this.contentCategoryService.saveSelective(record);
			
			// 判断父节点是否为叶子节点
			ContentCategory contentCategory = this.contentCategoryService.queryById(parentId);
			if(!contentCategory.getIsParent()){
				contentCategory.setIsParent(true);
				this.contentCategoryService.updateSelective(contentCategory);
			}
			return ResponseEntity.status(HttpStatus.CREATED).body(record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 重命名
	 * @param id
	 * @param name
	 * @return
	 */
	@PutMapping
	public ResponseEntity<Void> update(@RequestParam("id")Long id, @RequestParam("name")String name){
		
		try {
			ContentCategory record = new ContentCategory();
			record.setId(id);
			record.setName(name);
			int count = this.contentCategoryService.updateSelective(record);
			if(count == 1){
				return ResponseEntity.noContent().build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 删除节点
	 * @param id
	 * @param parentId
	 * @return
	 */
	@DeleteMapping
	public ResponseEntity<Void> delete(@RequestParam("id")Long id, @RequestParam("parentId")Long parentId){
		
		try {
			this.contentCategoryService.deleteContentCategory(id, parentId);
			return ResponseEntity.noContent().build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
